/*-------------------------------------------------------------------------
 *
 *	  UTF8 和西里尔字母
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/utf8_and_cyrillic/utf8_and_cyrillic.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"
#include "../../Unicode/utf8_to_koi8r.map"
#include "../../Unicode/koi8r_to_utf8.map"
#include "../../Unicode/utf8_to_koi8u.map"
#include "../../Unicode/koi8u_to_utf8.map"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(utf8_to_koi8r);
PG_FUNCTION_INFO_V1(koi8r_to_utf8);

PG_FUNCTION_INFO_V1(utf8_to_koi8u);
PG_FUNCTION_INFO_V1(koi8u_to_utf8);

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */

Datum utf8_to_koi8r(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8R);

	fc_converted = UtfToLocal(fc_src, fc_len, fc_dest,
						   &koi8r_from_unicode_tree,
						   NULL, 0,
						   NULL,
						   PG_KOI8R,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum koi8r_to_utf8(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8R, PG_UTF8);

	fc_converted = LocalToUtf(fc_src, fc_len, fc_dest,
						   &koi8r_to_unicode_tree,
						   NULL, 0,
						   NULL,
						   PG_KOI8R,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum utf8_to_koi8u(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_UTF8, PG_KOI8U);

	fc_converted = UtfToLocal(fc_src, fc_len, fc_dest,
						   &koi8u_from_unicode_tree,
						   NULL, 0,
						   NULL,
						   PG_KOI8U,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum koi8u_to_utf8(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_KOI8U, PG_UTF8);

	fc_converted = LocalToUtf(fc_src, fc_len, fc_dest,
						   &koi8u_to_unicode_tree,
						   NULL, 0,
						   NULL,
						   PG_KOI8U,
						   fc_noError);

	PG_RETURN_INT32(fc_converted);
}
